<template>
  <el-dialog title="json表单" :visible.sync="dialogFormVisible" :fullscreen="true">
    <el-form :model="form">
      <el-form-item label="项目名称" :label-width="formLabelWidth">
        <el-input v-model="form.project_title" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="项目创建者" :label-width="formLabelWidth">
        <el-input v-model="form.project_creater" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="项目更新者" :label-width="formLabelWidth">
        <el-input v-model="form.project_updater" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="项目url_id" :label-width="formLabelWidth">
        <el-input v-model="form.link_id" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="项目url" :label-width="formLabelWidth">
        <el-input v-model="form.link_url"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false" size="mini">取 消</el-button>
      <el-button type="primary" @click="onOk" size="mini">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'ModalForm',
  props: {
    modalForm: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      dialogFormVisible: false,
      formLabelWidth: '120px',
      form: {
        id: '',
        project_title: '',
        project_creater: '',
        project_updater: '',
        link_id: '',
        link_url: ''
      }
    }
  },
  methods: {
    open () {
      this.dialogFormVisible = true
    },
    onOk () {
      // eslint-disable-next-line no-eval
      this.form.content = JSON.stringify(this.form.content)
      this.dialogFormVisible = false
      this.$emit('save', this.form)
    },
    reset () { this.$refs.JsonEditor.reset() },
    onJsonChange (v) {
      this.form.content = v
    },
    onJsonSave (e) { console.log('Save:', e) },
    onError (e) { console.log('value:', e) }
  },
  watch: {
    modalForm (val) {
      // eslint-disable-next-line no-eval
      let obj = eval('(' + val.content + ')')
      val.content = obj
      this.form = { ...val }
    }
  }
}
</script>
